﻿@{
    ViewBag.Title = "字典编辑";
    Layout = "~/Views/Shared/_LayoutPageBaseEdit.cshtml";
}

@model EIP.System.Models.Dtos.Config.SystemDictionaryEditOutput
<div class="box-body">
    <div class="col-md-12">
        <div class="form-group">
            <label class="col-sm-2 control-label">上级</label>
            <div class="col-sm-10">
                <div class="input-group">
                    <input type="text" name="parentName" id="parentName" value="@Model.ParentName" class="form-control" data-pure-clear-button placeholder="上级">
                    <span id="menuBtn" onclick=" showDic(); return false; " class="input-group-addon pointer"><i class="fa fa-image"></i></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label m">名称</label>
            <div class="col-sm-10">
                <input type="text" name="name" id="name" value="@Model.Name" data-pure-clear-button class="form-control" placeholder="名称">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label m">代码</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" value="@Model.Code" data-pure-clear-button id="code" name="code" placeholder="代码">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">值</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="value" value="@Model.Value" data-pure-clear-button placeholder="值">
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-2 control-label m">排序号</label>
            <div class="col-sm-10">
                <input type="text" value="@Model.OrderNo" name="orderNo" class="form-control" data-pure-clear-button placeholder="排序号">
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-2 control-label">备注</label>
            <div class="col-sm-10">
                <textarea class="form-control" name="remark" data-pure-clear-button placeholder="备注">@Model.Remark</textarea>
            </div>
        </div>
    </div>
</div>
@Html.HiddenFor(f => @Model.DictionaryId)
@Html.HiddenFor(f => @Model.ParentId)
@Html.HiddenFor(f => @Model.CanbeDelete)
<div class="combotree-popup dropdown-menu" id="dicContent" style="display:none;">
    <div class="ip-control">
        <div class="box-tools">
            <div class="input-group" style="width: 272px;padding-left:5px">
                <input type="text" id="searchDicKey" class="form-control input-sm" placeholder="请输入名称进行模糊查询...">
                <div class="input-group-btn">
                    <a class="btn btn-sm btn-default" id="searchDic"><i class="fa fa-search"></i></a>
                </div>
            </div>
        </div>
    </div>
    <ul id="dicTree" class="ztree" style="margin-top:0; height: 223px; overflow: auto"></ul>
</div>
<script>
    var form = null;
    $(function () {
        //初始化控件
        $(".select2").select2();
        //iCheck for checkbox and radio inputs
        $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
            checkboxClass: 'icheckbox_minimal-blue',
            radioClass: 'iradio_minimal-blue'
        });
        //Red color scheme for iCheck
        $('input[type="checkbox"].minimal-red, input[type="radio"].minimal-red').iCheck({
            checkboxClass: 'icheckbox_minimal-red',
            radioClass: 'iradio_minimal-red'
        });
        //Flat red color scheme for iCheck
        $('input[type="checkbox"].flat-red, input[type="radio"].flat-red').iCheck({
            checkboxClass: 'icheckbox_flat-green',
            radioClass: 'iradio_flat-green'
        });
        $(".icon-picker").iconPicker();
        $.each($("input:text, input:password, " +
                "input[type='email'], input[type='url'], input[type='search'], input[type='tel'], input[type='number'], " +
                "input[type='datetime'],textarea", $("#editform")),
            function (index, element) {
                var $element = $(element);
                if (($element.attr("data-pure-clear-button") == undefined || $element.attr("data-pure-clear-button") === 'false') && !$element.hasClass("has-pure-clear-button")) {
                    return;
                }
                $element.pureClearButton("create");
            });

        ValidformNeed();

        initForm();
    });

    function initForm() {
        //初始化控件
        form = $("#editform").form();
        //数据校验
        $("#editform").formValidation({
            message: '请输入有效值',
            trigger: 'blur',
            fields: {
                name: {
                    validators: {
                        notEmpty: {
                            message: '请输入姓名'
                        }
                    }
                },
                code: {
                    validators: {
                        notEmpty: {
                            message: '请输入代码'
                        }
                    }
                }
            }
        }).on('success.form.fv', function (e) {
            var data = form.getFormSimpleData();
            data["ParentId"] = data["parentName"] !== "" ? data["ParentId"] : "";
            UtilAjaxPostWait("/System/Dictionary/SaveDictionary", data, function (data) {
                if (DialogAjaxResult(data)) {
                    dicObj = { pId: $("#ParentId").val() };
                    loadTreeAndGrid();
                    //关闭
                    dialog.close();
                }
            });
            return false;
        });
    }

</script>

<script type="text/javascript">

    var setting = {
        view: {
            dblClickExpand: false
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onClick: onClick
        }
    };

    function onClick(e, treeId, treeNode) {
        $("#ParentId").val(treeNode.id);
        //加载数据
        $("#parentName").val(treeNode.name);
    }

    function showDic() {
        var cityObj = $("#parentName");
        var cityOffset = $("#parentName").offset();
        var editformOffset = $("#editform").offset();
        $("#dicContent").css({ left: cityOffset.left - editformOffset.left + "px", top: (cityOffset.top + cityObj.outerHeight() + 6) - editformOffset.top + "px" }).slideDown("fast");
        $("body").bind("mousedown", onBodyDown);
    }

    function hideMenu() {
        $("#dicContent").fadeOut("fast");
        $("body").unbind("mousedown", onBodyDown);
    }

    function onBodyDown(event) {
        if (!(event.target.id === "menuBtn" || event.target.id === "dicContent" || $(event.target).parents("#dicContent").length > 0)) {
            hideMenu();
        }
    }

    $(document).ready(function () {
        //判断是否具有筛选菜单条件
        var postUrl = "/Common/UserControl/GetDictionaryRemoveChildren";
        UtilAjaxPostAsync(postUrl, {
            id: $("#DictionaryId").val()
        }, function (data) {
            combotreeDicTreeObj = $.fn.zTree.init($("#dicTree"), setting, data);
        });
        $('#searchDic').on("click", function (e) {
            search($("#searchDicKey").val());
        });

        $('#searchDicKey').on("keyup", function (e) {
            search($(this).val());
        });
    });
    var combotreeDicTreeObj = null;

    function search(p) {
        console.log(p);
        if (combotreeDicTreeObj) {
            var rootNodes = combotreeDicTreeObj.getNodes(),
                allNodes = combotreeDicTreeObj.transformToArray(rootNodes),
                checkedNodes = combotreeDicTreeObj.getCheckedNodes(true);
            $.each(checkedNodes, function () {
                combotreeDicTreeObj.checkNode(this, false);
            });
            combotreeDicTreeObj.cancelSelectedNode();
            if (p) {
                combotreeDicTreeObj.hideNodes(allNodes);
                var nodes = combotreeDicTreeObj.getNodesByParamFuzzy('name', p),
                    parentNode,
                    o = {};
                $.each(nodes, function (i, node) {
                    //if (!node.isParent) {
                        parentNode = node.getParentNode();
                        o[node.tId] = node;
                        while (parentNode) {
                            o[parentNode.tId] || (o[parentNode.tId] = parentNode);
                            parentNode = parentNode.getParentNode();
                        //}
                    }
                    return true;
                });
                $.each(o, function (key, value) {
                    combotreeDicTreeObj.showNode(value);
                });
                $.each(rootNodes, function (i, node) {
                    node.isParent && o[node.tId] && combotreeDicTreeObj.expandNode(node, true, true);
                });
            } else {
                combotreeDicTreeObj.showNodes(allNodes);
                combotreeDicTreeObj.expandAll(false);
            }
        }
    }

</script>